package com.drivemanage.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OpenUtils {

    /**
     * 图片的根路径
     */
    public static final String PIC_PATH = "D:/tomcat/tomcat/webapps/DrivingMan/carPics/";

    public static boolean isEmpty(List list) {
        return null == list || 0 == list.size();
    }

    public static boolean isEmpty(Map map) {
        return null == map || map.isEmpty();
    }

    public static boolean isEmpty(Object o) {
        return null == o || "".equals(o);
    }

    /**
     * 判断传入的多个参数是否有空的,有则返回true,都不为空返回false
     * 
     * @param object
     * @return
     */
    public static boolean isEmpty(Object... object) {
        for (Object o : object) {
            if (isEmpty(o)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取当前时间
     * 
     * @return
     */
    public static String getSysDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    /**
     * 转换时间格式
     * 
     * @param time
     * @param pattern
     * @return
     */
    public static String TransDate(String time, String pattern, String toPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(time);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        sdf = new SimpleDateFormat(toPattern);
        return sdf.format(date);
    }

    /**
     * 生成Map
     * 
     * @param objects
     * @return
     */
    public static Map asMap(Object... objects) {
        Map m = new HashMap(objects.length / 2 + 1);
        int i = 0;
        for (; i < objects.length - 1; i += 2) {
            m.put(objects[i], objects[i + 1]);
        }

        if (i < objects.length) {
            m.put(objects[i], null);
        }
        return m;
    }

    /**
     * 生成车辆ID(10开头)
     * 
     * @return
     */
    public static String createCarID() {
        return createID("10");
    }

    /**
     * 生成订单ID(20开头)
     * 
     * @return
     */
    public static String createOrderID() {
        return createID("20");
    }

    /**
     * 生成留言ID
     * 
     * @return
     */
    public static String createMassageID() {
        return createID("msg");
    }

    /**
     * 生成游记ID
     * 
     * @return
     */
    public static String createTravelID() {
        return createID("30");
    }

    /**
     * 自动生成ID
     * 
     * @return
     */
    private static String createID(String flag) {
        return flag + getSysDate();
    }

    /**
     * 比较第一个时间参数是否大于第二个时间参数
     * 
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isBig(String time1, String time2) {
        return time1.compareTo(time2) > 0;
    }

    /**
     * 转换日期格式
     * 
     * @param datetime
     * @param pattern
     * @param field
     * @param amount
     * @return
     */
    public static String rollDateStr(String datetime, String pattern, int field, int amount) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(datetime);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.add(field, amount);
        date = cd.getTime();
        return sdf.format(date);
    }

    /**
     * @param fDate1
     * @param oDate1
     * @param pattern
     * @return
     */
    public static int daysOfTwo(String fDate1, String oDate1, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date fDate = null;
        Date oDate = null;
        try {
            fDate = sdf.parse(fDate1);
            oDate = sdf.parse(oDate1);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(fDate);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(oDate);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return day2 - day1;
    }
    /**
     * 处理查询参数
     * @param name
     * @param type
     * @param price
     * @param t
     * @param ordertype
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public Map changeParam(String name, String type, String price, String t,
			String ordertype) {
		Object minprice=0;
		Object maxprice=0;
		Object mint=0;
		Object maxt=0;
		@SuppressWarnings("rawtypes")
		Map param=new HashMap();
		ordertype=ordertype.substring(0,ordertype.indexOf("\n"));
		//1.处理参数
		if("默认排序".equals(ordertype)){
			param.put("ordertype", "0");
		}
		if("价格".equals(ordertype)){
			param.put("ordertype", "1");
		}
		if("不限".equals(name)){
			name=null;
		}
		if("不限".equals(type)){
			type=null;
		}
		param.put("name", name);
		param.put("type", type);
		if("不限".equals(price)){
			price=null;
		}
		param.put("price", price);
		if("不限".equals(t)){
			t=null;
		}
		param.put("t", t);
		if("三万以下".equals(price)){
			minprice=0;
			maxprice=3;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("3-5万".equals(price)){
			minprice=3;
			maxprice=5;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("5-7万".equals(price)){
			minprice=5;
			maxprice=7;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("7-9万".equals(price)){
			minprice=7;
			maxprice=9;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("9-12万".equals(price)){
			minprice=9;
			maxprice=12;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("12-16万".equals(price)){
			minprice=12;
			maxprice=16;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("16-20万".equals(price)){
			minprice=16;
			maxprice=20;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("20万以上".equals(price)){
			minprice=20;
			maxprice=9999;
			param.put("minprice", minprice);
			param.put("maxprice", maxprice);
		}
		if("1.0L以下".equals(t)){
			mint=0;
			maxt=1.0;
			param.put("mint", mint);
			param.put("maxt", maxt);
		}
		if("1.0-1.6L".equals(t)){
			mint=1.0;
			maxt=1.6;
			param.put("mint", mint);
			param.put("maxt", maxt);
		}
		if("1.6-2.0L".equals(t)){
			mint=1.6;
			maxt=2.0;
			param.put("mint", mint);
			param.put("maxt", maxt);
		}
		if("2.0-3.0L".equals(t)){
			mint=2.0;
			maxt=3.0;
			param.put("mint", mint);
			param.put("maxt", maxt);
		}
		if("3.0L以上".equals(t)){
			mint=3.0;
			maxt=9.0;
			param.put("mint", mint);
			param.put("maxt", maxt);
		}
		return param;
	}
}
